<template>
  <!-- 更新密码：改
       1：数据回显
          a:表格内包含需要编辑数据，深拷贝处理回显
          b:表格内不包含要编辑数据（没有要编辑的数据），调用接口
       2：实现修改
          a:表单处理
          b:修改接口调用完成后
             包含修改接口定义与导入
              a:提示
              b:刷新数据
 -->
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form ref="form" label-width="100px" class="form" :model="form" :rules="rules">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="form.password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>

        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy><Info :user-info="initUserinfo" @getData="getData" /></el-tab-pane>
        <el-tab-pane label="岗位信息" lazy><Job /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'
export default {
  components: {
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data() {
    return {
      userId: this.$route.params.id,
      form: {
        username: '', //	string	非必须
        password: '' //	string	非必须
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        password: [{ required: true, message: '必填', trigger: 'change' }, {
          min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change'
        }] //	string	非必须
      },
      userInfo: {},
      initUserinfo: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.userId)
      this.form.username = res.data.username
      this.userInfo = res.data
      this.initUserinfo = JSON.parse(JSON.stringify(res.data))
      console.log('用户信息', res)
    },
    async submit() {
      await this.$refs.form.validate()
      await sysUserPut({
        ...this.userInfo,
        ...this.form
      })
      this.$message.success('更新成功')
      //   刷新数据（可能 是当前登录帐号）
      if (this.userId === this.$store.state.user.userInfo.id) {
        // 刷新用户信息
        this.$store.dispatch('user/getUserInfo')
      }
      // 更新Info组件的userInfo数据
      this.getData()
    }
  }
}
</script>
<style lang="scss" scoped>
.detail{
    padding: 20px;
    .form{
        width:500px;
        margin:40px 0 0 100px;
    }
}
</style>
